
//
// 121108 - AH - Created.
// 121110 - AH - Changes related to that WorldValidator tells names on tests it
//               is for.
//

package org.japril.jhut.framework;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 *   Object that associates world object with
 *   {@link WorldValidator}. 
 * </p>
 * @author Arne Halvorsen (AH)
 */
public final class WorldHandler
{
  private final Object m_world;
  
  private final Map<String, WorldValidator> m_validators=
    new HashMap<String, WorldValidator>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param world World object.
   */
  public WorldHandler(Object world)
  {
    if (world==null)
    {
      throw new NullPointerException("null");
    }
    
    m_world=world;
  }
  
  /**
   * <p>
   *   Gets world object.
   * </p>
   * @return World.
   */
  public Object getWorld(){ return m_world; }
  
  /**
   * <p>
   *   Sets
   *   {@link WorldValidator}.
   * </p>
   * @param wv {@link WorldValidator} to set.
   */
  public void setValidator(WorldValidator wv)
  {
    if (wv==null)
    {
      throw new NullPointerException("wv");
    }
    
    Set<String> names=wv.getTestNames();
    
    for (String name : names) m_validators.put(name, wv);
  }
  
  /**
   * <p>
   *   Removes validator.
   * </p>
   * @param tests Names on tests to remove validator for.
   */
  public void removeValidator(String... tests)
  {
    if (tests==null)
    {
      throw new NullPointerException("tests");
    }
    
    int n=tests.length;
    if (n==0)
    {
      throw new IllegalArgumentException("tests.length==0");
    }
    
    for (int i=0; i<n; i++)
    {
      String curr=tests[i];
      if (curr==null)
      {
        throw new NullPointerException("curr["+i+"]");
      }
      
      m_validators.remove(curr);
    }
  }
  
  /**
   * <p>
   *   Removes all validators.
   * </p>
   */
  public void clear(){ m_validators.clear(); }
  
  /**
   * <p>
   *   Gets number of validators.
   * </p>
   * @return Count.
   */
  public int size(){ return m_validators.size(); }
  
  /**
   * <p>
   *   Gets names of tests that has validators.
   * </p>
   * @return Names.
   */
  public List<String> getTestNames()
  {
    return new ArrayList<String>(m_validators.keySet());
  }
  
  /**
   * <p>
   *   Gets
   *   {@link WorldValidator}.
   * </p>
   * @param test Name on test validator is for.
   * @return {@link WorldValidator} or {@code null} if not found.
   */
  public WorldValidator getValidator(String test)
  {
    if (test==null)
    {
      throw new NullPointerException("test");
    }
    
    return m_validators.get(test);
  }
  
  @Override
  public String toString()
  {
    CharArrayWriter caw=new CharArrayWriter();
    PrintWriter pw=new PrintWriter(caw);
    
    pw.println("World:");
    pw.println("----");
    pw.println(m_world);
    pw.println("----");
    pw.println("Validators:");
    pw.println("----");
    for (Map.Entry<String, WorldValidator> curr : m_validators.entrySet())
    {
      String test=curr.getKey();
      WorldValidator wv=curr.getValue();
      pw.println("test="+test+", validator="+wv);
    }
    pw.println("----");
    
    
    return caw.toString();
  }
  
}
